import variable from "../config/app.js";
let hot = (main, numsFun, errFun) => {
  let url = variable.HTTP_REQUEST_URL;
  let hot = variable.hot;
  let header = variable.HEADER;
  if (hot.update) {
    upload(url, hot, header, main, numsFun, errFun);
  }
};
let upload = (url, hot, header, main, numsFun, errFun) => {
  var type;
  if (uni.getSystemInfoSync().platform === "android") {
    type = "1";
  } else {
    type = "2";
  }
  var http = url + hot.url;
  let platform = uni.getSystemInfoSync().platform;

  uni.request({
    url: http,
    data: {},
    method: hot.type,
    header: header,
    success: (res) => {
      // console.log(res)
      if (res.statusCode == 200) {
        if (res.data.code == 1) {
          var data = res.data.data;
          var versionNumber;
          let url = type == 1 ? data.android_url : data.ios_url;
          if (platform === "android") {
            versionNumber = hot.versionNumberAnd;
          } else {
            versionNumber = hot.versionNumberIos;
          }

          versionNumber = versionNumber.split(".").join("");
          let version =
            type == 1
              ? data.android_version.split(".").join("")
              : data.ios_version.split(".").join("");
          if (parseInt(version) > parseInt(versionNumber)) {
            //#ifdef APP-PLUS
            uni.showModal({
              content:
                "最新版本为:" +
                (type == 1 ? data.android_version : data.data.ios_version) +
                ",是否更新",
              title: "提示",
              showCancel: platform === "android" ? true : false,
              success(ress) {
                if (ress.confirm) {
                  if (platform === "android") {
                    download(url, numsFun);
                  } else {
                    openStore();
                  }
                } else if (ress.cancel) {
                  plus.runtime.quit();
                  errFun();
                }
              },
            });
            //#endif
          } else {
            errFun();
            if (main) {
              uni.showToast({
                title: "当前已是最新版",
                icon: "none",
              });
            }
          }
        }
      }
    },
  });
};

let openStore = () => {
  let appleId = 6496202553;
  plus.runtime.launchApplication(
    {
      action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`,
    },
    function (e) {
      console.log("Open system default browser failed: " + e.message);
    }
  );
};

let download = (url, numsFun) => {
  // uni.showLoading({
  // 	title: '下载中',
  // 	mask: true,
  // });
  const downloadTask = uni.downloadFile({
    url: url,
    success: (downloadResult) => {
      // uni.hideLoading();
      if (downloadResult.statusCode === 200) {
        plus.runtime.install(
          downloadResult.tempFilePath,
          {
            force: false,
          },
          function () {
            plus.runtime.restart();
          },
          function (e) {}
        );
      }
    },
  });
  downloadTask.onProgressUpdate((res) => {
    numsFun(res);
  });
};
export default hot;
